package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Map;
import com.itheima.service.MapService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/map")
public class MapController {
    @Reference
    private MapService mapService;

    //查询地图中的坐标点
    @RequestMapping("/findMap")
    public Result findMap() {
        List<Map> lsit = mapService.findMap();
        return new Result(true, "地址请求成功", lsit);
    }


    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean pageBean) {
        return mapService.findPage(pageBean);
    }


    //根据地址删除检查项
    @RequestMapping("/delete")
    @PreAuthorize("hasAuthority('PERMISSION_DELETE')")
    public Result delete(@RequestBody java.util.Map map) {
        try {
            System.out.println((String) map.get("address"));
            mapService.delete((String) map.get("address"));//发送请求
            return new Result(true, MessageConstant.DELETE_ADDRESS_SUCCESS);
        } catch (Exception e) {
            String message = e.getMessage();
            e.printStackTrace();
            return new Result(false, message);
        }
    }


    //新增
    @RequestMapping("/addAddress")
    @PreAuthorize("hasAuthority('PERMISSION_ADD')")
    public Result add(@RequestBody Map map) {
        try {
            mapService.addAddress(map);//发送请求
            return new Result(true, MessageConstant.ADD_ADDRESS_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }
    }
}
